<template>
  <el-form :model="form" label-width="180px" v-loading="isLoading">
    <el-form-item label="密码最短长度">
      <el-input-number
        v-model="form.requiredLength"
        :min="6"
        :max="64"
        :precision="0"
        :step="1"
      />
      <span style="margin-left: 20px">(6 - 64)</span>
    </el-form-item>
    <el-form-item label="最少不同字符">
      <el-input-number
        v-model="form.requiredUniqueChars"
        :min="0"
        :precision="0"
        :step="1"
      />
      <span style="margin-left: 20px">(0表示不校验)</span>
    </el-form-item>
    <el-form-item label="包含非字母数字字符">
      <el-switch v-model="form.requireNonAlphanumeric" />
    </el-form-item>
    <el-form-item label="包含小写字母">
      <el-switch v-model="form.requireLowercase" />
    </el-form-item>
    <el-form-item label="包含大写字母">
      <el-switch v-model="form.requireUppercase" />
    </el-form-item>
    <el-form-item label="包含数字">
      <el-switch v-model="form.requireDigit" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit"> 保存 </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'

import { getPasswordRule, setPasswordRule } from '@/http/rule'

const form = ref({
  requiredLength: 6,
  requiredUniqueChars: 0,
  requireNonAlphanumeric: true,
  requireLowercase: true,
  requireUppercase: true,
  requireDigit: true,
})
const isLoading = ref(false)

onMounted(() => {
  getData()
})

const getData = async () => {
  isLoading.value = true
  const res = await getPasswordRule().finally(() => {
    isLoading.value = false
  })
  form.value = res
}

const submit = async () => {
  isLoading.value = true
  await setPasswordRule(form.value).finally(() => {
    isLoading.value = false
  })
  ElMessage.success('保存成功')
}
</script>

<style scoped lang="scss"></style>
